<template>
  <div class="page-warpper">
    <el-card>
      <el-row type="flex" justify="end" style="margin-bottom:20px">
        <el-col :span="2">
          <el-button type="primary" size="small" @click="isShowDialog=true">添加权限</el-button>
        </el-col>
      </el-row>
      <div style="margin-bottom:10px;color:#f5f7fa">
        <hr>
      </div>
      <el-table border="" :data="list">
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="邮箱" prop="email" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button type="primary" size="mini" @click="setRole(row.id)">设置角色</el-button>
            <el-button size="mini" @click="editRole(row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteRole(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination :total="total" :page-size="pageConfig._limit" @current-change="currentChange" />
      </el-row>
    </el-card>
    <!-- 添加弹窗 -->
    <el-dialog :visible="isShowDialog" :title="formData.id?'编辑权限':'添加权限'" @close="btnCancel">
      <el-form ref="addForm" :model="formData" label-position="left" label-width="70px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="btnOk">确定</el-button>
        <el-button @click="btnCancel">取消</el-button>
      </el-row>
    </el-dialog>
    <!-- 设置角色 -->
    <SetRole :id="id" :is-show-check="isShowCheck" @closeDialog="isShowCheck=false" />
  </div>
</template>

<script>
import SetRole from './setRole/index.vue'
import { getList, getCount, addEmployee, employeeDatil, editEmployee, delEmployee } from '@/api/employee'
export default {
  components: {
    SetRole
  },
  data() {
    return {

      list: [],
      total: 0,
      pageConfig: {
        _limit: 2,
        _start: 0
      },
      formData: {
        username: '',
        email: '',
        password: ''
      },
      isShowDialog: false,
      isShowCheck: false,
      rules: {
        username: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      id: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      const res = await getList(this.pageConfig)
      console.log(res)
      this.list = res.data
      const count = await getCount()
      this.total = count.data
      console.log(count)
    },
    // 切换页面
    currentChange(page) {
      const lastPage = page - 1
      const size = lastPage * this.pageConfig._limit
      this.pageConfig._start = size
      // 更新列表
      this.getList()
    },
    btnCancel() {
      this.$refs.addForm.resetFields()
      this.formData = {
        username: '',
        email: '',
        password: ''
      }
      this.isShowDialog = false
    },
    // 添加逻辑
    async btnOk() {
      await this.$refs.addForm.validate()
      // 编辑逻辑
      if (this.formData.id) {
        await editEmployee(this.formData)
        this.$message.success('编辑成功')
      } else {
        // 添加逻辑
        const res = await addEmployee(this.formData)
        console.log('添加', res)
        this.$message.success('添加成功')
      }
      this.isShowDialog = false
      this.getList()
    },
    setRole(id) {
      this.id = id
      this.isShowCheck = true
    },
    async deleteRole(id) {
      await this.$confirm('你确定要删除吗？')
      const res = await delEmployee(id)
      console.log(res)
      this.$message.success('删除成功')
      this.getList()
    },
    // 编辑回填
    async editRole(id) {
      const res = await employeeDatil(id)
      console.log(res)
      this.formData = res.data
      this.isShowDialog = true
    }
  }

}
</script>

<style lang="scss" scoped>
.page-warpper{
    margin: 20px;
}
</style>
